package Dao;

import Vo.Post;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Date;
import java.util.Random;

public class PostDao {
    private Connection conn = null;
    private static int postno=1001;
    public void initConnection() throws Exception {
        //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Class.forName("com.mysql.cj.jdbc.Driver");
        //conn = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=school", "sa", "123456");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/forum", "root", "root");
    }
    public HashMap getallpost()throws Exception{
        HashMap hm=new HashMap();
        this.initConnection();
        Statement statement=conn.createStatement();
        String sql = "SELECT Pid,title,titlepic,content,createdtime,tabid,user.Uid,type,tags,hits,username FROM post join user on post.Uid = user.Uid";
        ResultSet resultSet=statement.executeQuery(sql);
        while (resultSet.next()){
            Post post=new Post();
            post.setPid(resultSet.getString("Pid"));
            post.setTitle(resultSet.getString("title"));
            post.setTitlepic(resultSet.getString("titlepic"));
            post.setContent(resultSet.getString("content"));
            post.setCreatedtime(resultSet.getString("createdtime"));
            post.setTabid(resultSet.getString("tabid"));
            post.setUid(resultSet.getString("Uid"));
            post.setType(resultSet.getInt("type"));
            post.setTags(resultSet.getString("tags"));
            post.setHits(resultSet.getInt("hits"));
            post.setUsername(resultSet.getString("username"));
            hm.put(post.getPid(),post);
        }
        closeConnection();
        return hm;
    }
    public Post getPostbyPid(String Pid) throws Exception {
        Post post=null;
        initConnection();
        String sql = "SELECT Pid,title,titlepic,content,createdtime,tabid,user.Uid,type,tags,hits,username From post join user on post.Uid=user.Uid WHERE Pid=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, Pid);
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            post = new Post();
            post.setPid(rs.getString("Pid"));
            post.setTitle(rs.getString("title"));
            post.setTitlepic(rs.getString("titlepic"));
            post.setContent(rs.getString("content"));
            post.setCreatedtime(rs.getString("createdtime"));
            post.setTabid(rs.getString("tabid"));
            post.setUid(rs.getString("Uid"));
            post.setType(rs.getInt("type"));
            post.setTags(rs.getString("tags"));
            post.setHits(rs.getInt("hits"));
            post.setUsername(rs.getString("username"));
//			System.out.println("uid:"+user.getUid());
//			System.out.println("password:"+user.getPassword());
//			System.out.println("username:"+user.getUsername());
        }
        closeConnection();
        return post;
    }
    public void postMessage(String Pid,String Uid,String title,String content) throws Exception{
        initConnection();
        java.util.Date  date=new java.util.Date();
        java.sql.Date  sqldate=new java.sql.Date(date.getTime());
        String sql="insert into post(Pid,title,titlepic,createdtime,tabid,Uid,type,tags,hits,content) values(?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1,Pid);
        ps.setString(2,title);
        ps.setString(3,"0");
        ps.setDate(4, sqldate);
        ps.setString(5,"0");
        ps.setString(6,Uid);
        ps.setInt(7,0);
        ps.setString(8,"0");
        ps.setInt(9,0);
        ps.setString(10,content);
        ps.executeUpdate();
        closeConnection();
        System.out.println("Pid:"+Pid);
        System.out.println("title:"+title);
        System.out.println("createdtime:"+sqldate);
        System.out.println("Uid:"+Uid);
        System.out.println("content:"+content);
        System.out.println("hits:"+0);
    }
    public void addhits(String Pid)throws Exception{
        initConnection();
        String sql="update Post set hits=hits+1 where Pid=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1,Pid);
        ps.executeUpdate();
        closeConnection();
    }
    public void closeConnection() throws Exception {
        conn.close();
    }
}
